from app_user.utls import bootstrap
from app_user.models import UserInfo, Address,CommentInfo,Goods
from django.core.exceptions import ValidationError
from django import forms
from app_user.utls.encrypt import md5

class LoginModeForm(bootstrap.BootstrapForm):

    name = forms.CharField(
        label='账号',
        widget=forms.TextInput
    )

    password = forms.CharField(
        label='密码',
        widget=forms.PasswordInput(render_value=True)
    )

    code = forms.CharField(
        label='验证码',
        widget=forms.TextInput
    )

    def clean_password(self):
        pwd = self.cleaned_data.get('password')
        return md5(pwd)





class RegisterModeForm(bootstrap.BootstrapModeForm):
    confirm_password = forms.CharField(label='确认密码', widget= forms.PasswordInput(render_value=True))
    class Meta:
        model = UserInfo
        fields = ['password','confirm_password',  'name', 'email']


    def clean_password(self):
        """"""
        """加密密码"""

        pwd = md5(self.cleaned_data.get('password'))

        return pwd
    def clean_confirm_password(self):
        """"""
        """检验密码是否一致"""

        pwd = self.cleaned_data.get('password')
        confirm_pwd = md5(self.cleaned_data.get('confirm_password'))

        if pwd != confirm_pwd:
            print(pwd, confirm_pwd)
            raise ValidationError("密码不一致")

        return confirm_pwd

    def clean_name(self):
        """"""
        """检查账户是否存在"""
        txt_name = self.cleaned_data.get('name')
        if UserInfo.objects.filter(name=txt_name).exists():
            raise ValidationError("账户已存在")
        return txt_name


class Email_authenticationModeForm(bootstrap.BootstrapModeForm):

    code = forms.CharField(
        label='验证码',
        widget=forms.TextInput
    )
    class Meta:
        model = UserInfo
        fields = ['email']





class edit_user_inforModeForm(bootstrap.BootstrapModeForm):
    """"""
    """完善用户信息"""

    class Meta:
        model = UserInfo
        fields = "__all__"



class edit_goods_ModeForm(bootstrap.BootstrapModeForm):
    """"""
    """商品信息"""

    class Meta:
        model = Goods
        fields = "__all__"



class user_address_add_ModeForm(bootstrap.BootstrapModeForm):
    """"""
    """收货地址"""



    class Meta:
        model = Address
        exclude = ['is_default', 'user']



class commentadd_ModeForm(bootstrap.BootstrapModeForm):
    """"""
    """添加评论"""


    class Meta:
        model = CommentInfo
        exclude = ['goods', 'user', 'create_time']







